package cm.tl.pms.common.entity;

import lombok.Data;

@Data
public class ResultMsg<T> {

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 返回的消息
     */
    private String resultMsg;

    /**
     * 应答码
     */
    private String resultCode;

    /**
     * 返回值，类型为T
     */
    private T data;


    /**
     * 根据操作是否成功来生成相应的结果消息。
     *
     * @param isSuccess 指示操作是否成功的布尔值。如果操作成功，该参数为true；如果操作失败，该参数为false。
     * @param <T>       结果消息中承载的数据的类型。
     * @return ResultMsg<T> 类型的结果消息对象。如果操作成功，返回一个包含成功标识和相关数据的成功结果消息；如果操作失败，返回一个包含失败标识和空数据的失败结果消息。
     */
    public static <T> ResultMsg<T> result(boolean isSuccess) {
        return isSuccess ? ResultMsg.successfulResult() : ResultMsg.failResult();
    }

    public static <T> ResultMsg<T> result(boolean isSuccess, String message) {
        return isSuccess ? ResultMsg.successfulResult() : ResultMsg.failResult(message);
    }

    public static <T> ResultMsg<T> result(boolean isSuccess, ConverterBaseEnum data) {
        return isSuccess ? ResultMsg.successfulResult() : ResultMsg.failResult(data);
    }

    /**
     * 失败返回方法
     */
    public static <T> ResultMsg<T> failResult(ConverterBaseEnum data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(false);
        result.setResultCode(data.getCode());
        result.setResultMsg(data.getMsg());
        return result;
    }

    /**
     * 失败返回方法 动态返回自定义信息
     */
    public static <T> ResultMsg<T> failResult(String message) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(false);
        result.setResultCode(ErrorEnum.FALSE.getCode());
        result.setResultMsg(message);
        return result;
    }

    /**
     * 失败返回方法 动态返回自定义信息
     */
    public static <T> ResultMsg<T> failResult(String code, String message, T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(false);
        result.setResultCode(code);
        result.setResultMsg(message);
        result.setData(data);
        return result;
    }

    /**
     * 失败返回方法 返回自定义信息
     */
    public static <T> ResultMsg<T> failResult(String code, String message) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(false);
        result.setResultCode(code);
        result.setResultMsg(message);
        return result;
    }

    public static <T> ResultMsg<T> failResult() {
        return failResult("");
    }

    public static <T> ResultMsg<T> successfulResult() {
        return successfulResult(ErrorEnum.SUCCESS.getMsg());
    }

    /**
     * 成功返回方法
     */
    public static <T> ResultMsg<T> successfulResult(T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(ErrorEnum.SUCCESS.getCode());
        result.setResultMsg(ErrorEnum.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    /**
     * 成功返回方法
     */
    public static <T> ResultMsg<T> successfulResultDataStr(T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(ErrorEnum.SUCCESS.getCode());
        result.setResultMsg(ErrorEnum.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    /**
     * 成功返回枚举类方法
     */
    public static <T> ResultMsg<T> successfulResult(ConverterBaseEnum data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(data.getCode());
        result.setResultMsg(data.getMsg());
        return result;
    }

    /**
     * 成功返回枚举类方法
     */
    public static <T> ResultMsg<T> successStrResult(T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(ErrorEnum.SUCCESS.getCode());
        result.setResultMsg(ErrorEnum.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> ResultMsg<T> data(T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(ErrorEnum.SUCCESS.getCode());
        result.setResultMsg(ErrorEnum.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    /**
     * 成功返回枚举类方法
     */
    public static <T> ResultMsg<T> successfulResult(String msg) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(ErrorEnum.SUCCESS.getCode());
        result.setResultMsg(msg);
        return result;
    }

    /**
     * 成功返回信息，加数据
     */
    public static <T> ResultMsg<T> successfulResult(String msg, T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(ErrorEnum.SUCCESS.getCode());
        result.setResultMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 成功返回方法 动态返回自定义信息
     */
    public static <T> ResultMsg<T> successfulResult(String code, String message, T data) {
        ResultMsg<T> result = new ResultMsg<>();
        result.setSuccess(true);
        result.setResultCode(code);
        result.setResultMsg(message);
        result.setData(data);
        return result;
    }

}
